home *** CD-ROM | disk | FTP | other *** search
/ CD Actual Thematic 7: Programming / CDAT7.iso / demos / VisualAge for Java 2.0 Entry / setup / data1.cab / ide-e / IDE / cache / 1UIE8HC (.txt) < prev    next >
Encoding:
Java Class File  |  1998-09-16  |  7.4 KB  |  339 lines

  1. package com.sun.java.swing;
  2.  
  3. import com.sun.java.accessibility.Accessible;
  4. import com.sun.java.accessibility.AccessibleContext;
  5. import java.awt.Color;
  6. import java.awt.Component;
  7. import java.awt.Container;
  8. import java.awt.Graphics;
  9. import java.awt.LayoutManager;
  10. import java.awt.Rectangle;
  11. import java.util.Hashtable;
  12.  
  13. public class JLayeredPane extends JComponent implements Accessible {
  14.    public static final Integer DEFAULT_LAYER = new Integer(0);
  15.    public static final Integer PALETTE_LAYER = new Integer(100);
  16.    public static final Integer MODAL_LAYER = new Integer(200);
  17.    public static final Integer POPUP_LAYER = new Integer(300);
  18.    public static final Integer DRAG_LAYER = new Integer(400);
  19.    public static final Integer FRAME_CONTENT_LAYER = new Integer(-30000);
  20.    public static final String LAYER_PROPERTY = "layeredContainerLayer";
  21.    private Hashtable componentToLayer;
  22.    private boolean optimizedDrawingPossible = true;
  23.  
  24.    public JLayeredPane() {
  25.       ((Container)this).setLayout((LayoutManager)null);
  26.    }
  27.  
  28.    protected void addImpl(Component comp, Object constraints, int index) {
  29.       int layer = DEFAULT_LAYER;
  30.       if (constraints instanceof Integer) {
  31.          layer = (Integer)constraints;
  32.          this.setLayer(comp, layer);
  33.       } else {
  34.          layer = this.getLayer(comp);
  35.       }
  36.  
  37.       int pos = this.insertIndexForLayer(layer, index);
  38.       super.addImpl(comp, constraints, pos);
  39.       comp.validate();
  40.       comp.repaint();
  41.       this.validateOptimizedDrawing();
  42.    }
  43.  
  44.    public AccessibleContext getAccessibleContext() {
  45.       if (super.accessibleContext == null) {
  46.          super.accessibleContext = new AccessibleJLayeredPane(this);
  47.       }
  48.  
  49.       return super.accessibleContext;
  50.    }
  51.  
  52.    public int getComponentCountInLayer(int layer) {
  53.       int layerCount = 0;
  54.       int count = ((Container)this).getComponentCount();
  55.  
  56.       for(int i = 0; i < count; ++i) {
  57.          int curLayer = this.getLayer(((Container)this).getComponent(i));
  58.          if (curLayer == layer) {
  59.             ++layerCount;
  60.          } else if (layerCount > 0 || curLayer < layer) {
  61.             break;
  62.          }
  63.       }
  64.  
  65.       return layerCount;
  66.    }
  67.  
  68.    public Component[] getComponentsInLayer(int layer) {
  69.       int layerCount = 0;
  70.       Component[] results = new Component[this.getComponentCountInLayer(layer)];
  71.       int count = ((Container)this).getComponentCount();
  72.  
  73.       for(int i = 0; i < count; ++i) {
  74.          int curLayer = this.getLayer(((Container)this).getComponent(i));
  75.          if (curLayer == layer) {
  76.             results[layerCount++] = ((Container)this).getComponent(i);
  77.          } else if (layerCount > 0 || curLayer < layer) {
  78.             break;
  79.          }
  80.       }
  81.  
  82.       return results;
  83.    }
  84.  
  85.    protected Hashtable getComponentToLayer() {
  86.       if (this.componentToLayer == null) {
  87.          this.componentToLayer = new Hashtable(4);
  88.       }
  89.  
  90.       return this.componentToLayer;
  91.    }
  92.  
  93.    public int getIndexOf(Component c) {
  94.       int count = ((Container)this).getComponentCount();
  95.  
  96.       for(int i = 0; i < count; ++i) {
  97.          if (c == ((Container)this).getComponent(i)) {
  98.             return i;
  99.          }
  100.       }
  101.  
  102.       return -1;
  103.    }
  104.  
  105.    public static int getLayer(JComponent c) {
  106.       Integer i;
  107.       return (i = (Integer)c.getClientProperty("layeredContainerLayer")) != null ? i : DEFAULT_LAYER;
  108.    }
  109.  
  110.    public int getLayer(Component c) {
  111.       Integer i;
  112.       if (c instanceof JComponent) {
  113.          i = (Integer)((JComponent)c).getClientProperty("layeredContainerLayer");
  114.       } else {
  115.          i = (Integer)this.getComponentToLayer().get("layeredContainerLayer");
  116.       }
  117.  
  118.       return i == null ? DEFAULT_LAYER : i;
  119.    }
  120.  
  121.    public static JLayeredPane getLayeredPaneAbove(Component c) {
  122.       if (c == null) {
  123.          return null;
  124.       } else {
  125.          Component parent;
  126.          for(parent = c.getParent(); parent != null && !(parent instanceof JLayeredPane); parent = parent.getParent()) {
  127.          }
  128.  
  129.          return (JLayeredPane)parent;
  130.       }
  131.    }
  132.  
  133.    protected Integer getObjectForLayer(int layer) {
  134.       Integer layerObj;
  135.       switch (layer) {
  136.          case 0:
  137.             layerObj = DEFAULT_LAYER;
  138.             break;
  139.          case 100:
  140.             layerObj = PALETTE_LAYER;
  141.             break;
  142.          case 200:
  143.             layerObj = MODAL_LAYER;
  144.             break;
  145.          case 300:
  146.             layerObj = POPUP_LAYER;
  147.             break;
  148.          case 400:
  149.             layerObj = DRAG_LAYER;
  150.             break;
  151.          default:
  152.             layerObj = new Integer(layer);
  153.       }
  154.  
  155.       return layerObj;
  156.    }
  157.  
  158.    public int getPosition(Component c) {
  159.       int pos = 0;
  160.       ((Container)this).getComponentCount();
  161.       int startLocation = this.getIndexOf(c);
  162.       if (startLocation == -1) {
  163.          return -1;
  164.       } else {
  165.          int startLayer = this.getLayer(c);
  166.  
  167.          for(int i = startLocation - 1; i >= 0; --i) {
  168.             int curLayer = this.getLayer(((Container)this).getComponent(i));
  169.             if (curLayer != startLayer) {
  170.                return pos;
  171.             }
  172.  
  173.             ++pos;
  174.          }
  175.  
  176.          return pos;
  177.       }
  178.    }
  179.  
  180.    public int highestLayer() {
  181.       return ((Container)this).getComponentCount() > 0 ? this.getLayer(((Container)this).getComponent(0)) : 0;
  182.    }
  183.  
  184.    protected int insertIndexForLayer(int layer, int position) {
  185.       int layerStart = -1;
  186.       int layerEnd = -1;
  187.       int count = ((Container)this).getComponentCount();
  188.  
  189.       for(int i = 0; i < count; ++i) {
  190.          int curLayer = this.getLayer(((Container)this).getComponent(i));
  191.          if (layerStart == -1 && curLayer == layer) {
  192.             layerStart = i;
  193.          }
  194.  
  195.          if (curLayer < layer) {
  196.             if (i == 0) {
  197.                layerStart = 0;
  198.                layerEnd = 0;
  199.             } else {
  200.                layerEnd = i;
  201.             }
  202.             break;
  203.          }
  204.       }
  205.  
  206.       if (layerStart == -1 && layerEnd == -1) {
  207.          return count;
  208.       } else {
  209.          if (layerStart != -1 && layerEnd == -1) {
  210.             layerEnd = count;
  211.          }
  212.  
  213.          if (layerEnd != -1 && layerStart == -1) {
  214.             layerStart = layerEnd;
  215.          }
  216.  
  217.          if (position == -1) {
  218.             return layerEnd;
  219.          } else {
  220.             return position > -1 && layerStart + position <= layerEnd ? layerStart + position : layerEnd;
  221.          }
  222.       }
  223.    }
  224.  
  225.    public boolean isOptimizedDrawingEnabled() {
  226.       return this.optimizedDrawingPossible;
  227.    }
  228.  
  229.    public int lowestLayer() {
  230.       int count = ((Container)this).getComponentCount();
  231.       return count > 0 ? this.getLayer(((Container)this).getComponent(count - 1)) : 0;
  232.    }
  233.  
  234.    public void moveToBack(Component c) {
  235.       this.setPosition(c, this.getComponentCountInLayer(this.getLayer(c)));
  236.    }
  237.  
  238.    public void moveToFront(Component c) {
  239.       this.setPosition(c, 0);
  240.    }
  241.  
  242.    public void paint(Graphics g) {
  243.       if (((JComponent)this).isOpaque()) {
  244.          Rectangle r = g.getClipBounds();
  245.          Color c = ((Component)this).getBackground();
  246.          if (c == null) {
  247.             c = Color.lightGray;
  248.          }
  249.  
  250.          g.setColor(c);
  251.          g.fillRect(r.x, r.y, r.width, r.height);
  252.       }
  253.  
  254.       super.paint(g);
  255.    }
  256.  
  257.    public static void putLayer(JComponent c, int layer) {
  258.       Integer layerObj = new Integer(layer);
  259.       c.putClientProperty("layeredContainerLayer", layerObj);
  260.    }
  261.  
  262.    public void remove(int index) {
  263.       ((Container)this).getComponent(index);
  264.       super.remove(index);
  265.       this.validateOptimizedDrawing();
  266.    }
  267.  
  268.    public void setLayer(Component c, int layer) {
  269.       this.setLayer(c, layer, -1);
  270.    }
  271.  
  272.    public void setLayer(Component c, int layer, int position) {
  273.       Integer layerObj = this.getObjectForLayer(layer);
  274.       if (layer == this.getLayer(c) && position == this.getPosition(c)) {
  275.          if (c instanceof JComponent) {
  276.             ((JComponent)this).repaint(((JComponent)c)._bounds);
  277.          } else {
  278.             ((JComponent)this).repaint(c.getBounds());
  279.          }
  280.  
  281.       } else {
  282.          if (c instanceof JComponent) {
  283.             ((JComponent)c).putClientProperty("layeredContainerLayer", layerObj);
  284.          } else {
  285.             this.getComponentToLayer().put("layeredContainerLayer", layerObj);
  286.          }
  287.  
  288.          if (c.getParent() != null && c.getParent() == this) {
  289.             ((Container)this).remove(c);
  290.             ((Container)this).add(c, (Object)null, position);
  291.             if (c instanceof JComponent) {
  292.                ((JComponent)this).repaint(((JComponent)c)._bounds);
  293.             } else {
  294.                ((JComponent)this).repaint(c.getBounds());
  295.             }
  296.  
  297.          } else {
  298.             if (c instanceof JComponent) {
  299.                ((JComponent)this).repaint(((JComponent)c)._bounds);
  300.             } else {
  301.                ((JComponent)this).repaint(c.getBounds());
  302.             }
  303.  
  304.          }
  305.       }
  306.    }
  307.  
  308.    public void setPosition(Component c, int position) {
  309.       this.setLayer(c, this.getLayer(c), position);
  310.    }
  311.  
  312.    private void validateOptimizedDrawing() {
  313.       boolean layeredComponentFound = false;
  314.       synchronized(((Component)this).getTreeLock()){}
  315.  
  316.       try {
  317.          Integer layer = null;
  318.          int i = 0;
  319.  
  320.          for(int d = ((Container)this).getComponentCount(); i < d; ++i) {
  321.             layer = null;
  322.             if ((((Container)this).getComponent(i) instanceof JInternalFrame || ((Container)this).getComponent(i) instanceof JComponent && (layer = (Integer)((JComponent)((Container)this).getComponent(i)).getClientProperty("layeredContainerLayer")) != null) && (layer == null || !layer.equals(FRAME_CONTENT_LAYER))) {
  323.                layeredComponentFound = true;
  324.                break;
  325.             }
  326.          }
  327.       } catch (Throwable var7) {
  328.          throw var7;
  329.       }
  330.  
  331.       if (layeredComponentFound) {
  332.          this.optimizedDrawingPossible = false;
  333.       } else {
  334.          this.optimizedDrawingPossible = true;
  335.       }
  336.  
  337.    }
  338. }
  339.